package cn.tedu.nio.selector;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class Client {
    public static void main(String[] args) {
        try {
            // 开启客户端通道
            SocketChannel sc = SocketChannel.open();
            // 设置非阻塞
//            sc.configureBlocking(false);
            // 发起连接
            sc.connect(new InetSocketAddress("localhost", 8070));
            // 给服务器写数据
            sc.write(ByteBuffer.wrap("Hello server".getBytes()));
            // 接收服务器返回的数据
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            sc.read(buffer);
            System.out.println(new String(buffer.array(), 0, buffer.position()));
            // 关流
            sc.close();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }
}
